#define LOG_TAG "amp.tag"
#include "elog.h"

#include "amp_task.h"
#include "amp_m4.h"

osThreadId_t ampTaskHandle;
const osThreadAttr_t ampTask_attributes = {
    .name = "ampTask",
    .stack_size = 1024 + 512,
    .priority = (osPriority_t)osPriorityAboveNormal,
}; 

int amp_init(void)
{
	int ret = 0;
    if (!IS_ENGINEERING_BOOT_MODE())
    {
        /* 初始化虚拟串口 */
        if (VIRT_UART_Init(&huart0) != VIRT_UART_OK)
        {
            log_d("viri uart init fail.");
            ret = -1;
            goto __exit;
        }

        /* 注册接收回调函数 */
        if (VIRT_UART_RegisterCallback(&huart0, VIRT_UART_RXCPLT_CB_ID, VIRT_UART0_RxCpltCallback) != VIRT_UART_OK)
        {
            log_d("viri callback register fail.");
            ret = -2;
            goto __exit;
        }

        /* 创建发送的消息队列 */
        send_q = xQueueCreate(20u, sizeof(amp_send_msg_t)); /* 创建发送队列 */
        log_d("init amp success.");
    }
__exit:
    return ret;
}
// INIT_APP_EXPORT(amp_init);

/**
 * @brief                   核间通讯任务
 * @param  argument         Function Param doc
 */
void amptask(void *argument)
{
    amp_init();
    for (;;)
    {
        if (!IS_ENGINEERING_BOOT_MODE())
        {
            OPENAMP_check_for_message();
            if (flag)
            {
                flag = RESET;
                if (RxSize >= 3)
                {
                    receivmesg();
                }
            }
            if (ACKFLAG == 1)
            {
                if ((xTaskGetTickCount() - Ttickack) >= 1000)
                {
                    log_d("send rpmsg.");
                    sendmesg();
                    ACKFLAG = 0;
                }
            }
            else
                sendmesg();
        }
        osDelay(10);
    }
}
